from rest_framework import serializers

from Problems.models import Problem, ProblemTag
from Utils.GetRemoteUrl import get_remote_url


class ProblemTagSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemTag
        fields = (
            'name',
            'color',
        )


class ProblemSerializer(serializers.ModelSerializer):
    remote_oj = serializers.CharField(source='remote_oj.name')
    remote_url = serializers.SerializerMethodField()
    createTime = serializers.DateTimeField(source='create_time', read_only=True)
    updateTime = serializers.DateTimeField(source='update_time', read_only=True)
    tags = ProblemTagSerializer(many=True, read_only=True)

    class Meta:
        model = Problem
        fields = (
            'id',
            'remote_id',
            'title',
            'time_limit',
            'memory_limit',
            'total_submitted',
            'total_accepted',
            'createTime',
            'updateTime',
            'content',
            'is_spj',
            'remote_oj',
            'remote_url',
            'tags',
            'rating',
        )
        read_only_fields = ('id', 'last_updated',)

    def get_remote_url(self, obj):
        return get_remote_url(obj.remote_oj.name, obj.remote_id)


class ProblemDetailSerializer(serializers.ModelSerializer):
    remote_oj_name = serializers.SerializerMethodField()

    class Meta:
        model = Problem
        fields = (
            'id',
            'remote_oj_name',
            'title',
            'time_limit',
            'memory_limit',
            'is_spj',
            'rating',
            'remote_id',
            'content',
        )

    read_only_fields = ('id', 'last_updated', 'remote_oj_name',)

    def get_remote_oj_name(self, obj):
        return obj.remote_oj.name


class SubmitSerializer(serializers.Serializer):
    problemId = serializers.IntegerField()
    code = serializers.CharField(max_length=1024 * 8)
    lang = serializers.CharField()
